package com.android.djlmapp.tools.read;

import android.util.Log;

import com.android.djlmapp.http.GlobalUrls;
import com.android.djlmapp.listener.read.GetPhotoUrlListener;
import com.android.djlmapp.model.readcard.UploadImg;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * 单张照片上传
 */
public class GetPhotoUrlUtils {
    /**
     * 监听回调的值不为空才可以继续处理业务
     *
     * @param idPath
     * @param mGetPhotoUrlListener
     */
    public static void getPhotoUrl(String idPath, GetPhotoUrlListener mGetPhotoUrlListener) {
        Log.e("项目异常", "------getPhotoUrl------" + idPath);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(new File(idPath));
            FileChannel fc = fis.getChannel();
            Log.e("项目异常", "getPhotoUrlsize: " + fc.size());
        } catch (FileNotFoundException e) {
            mGetPhotoUrlListener.backPhotoUrl(2, "保存文件失败1");
            e.printStackTrace();
        } catch (IOException e) {
            mGetPhotoUrlListener.backPhotoUrl(2, "保存文件失败2");
            e.printStackTrace();
        }
        OkGo.<String>post(GlobalUrls.UPLOAD_IMG)
                .params("image", new File(idPath))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.e("项目异常", "------getPhotoUrl--onSuccess----" + response.body());
                        try {
                            if (response.body().contains("filename")) {
                                //解析JSON
                                Gson gson = new Gson();
                                UploadImg uploadImg = gson.fromJson(response.body(), UploadImg.class);
                                // 身份证头像的URL
                                String PhotoUrl = uploadImg.getFilename();
                                mGetPhotoUrlListener.backPhotoUrl(1, PhotoUrl);
                            } else {
                                mGetPhotoUrlListener.backPhotoUrl(2, "照片上传失败2");
                            }
                        } catch (Exception e) {
                            mGetPhotoUrlListener.backPhotoUrl(2, "数据解析异常2");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Log.e("项目异常", "------getPhotoUrl---onError---" + response.body());
                        mGetPhotoUrlListener.backPhotoUrl(2, "网络或服务器错误2");
                    }
                });


    }

    /**
     * ChooseActivity 单独调用
     * 监听回调的值不为空才可以继续处理业务
     *
     * @param url
     * @param file
     * @param mGetPhotoUrlListener
     */
    public static void subChoosePhoto(String url, File file, GetPhotoUrlListener mGetPhotoUrlListener) {
        Log.e("项目异常", "------getPhotoUrl------" + file.getPath());
        OkGo.<String>post(url)
                .params("image", file)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        mGetPhotoUrlListener.backPhotoUrl(1, "");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        mGetPhotoUrlListener.backPhotoUrl(2, "网络或服务器错误2");
                    }
                });


    }

}
